<?php

namespace App\Http\ApiControllers;

use App\Models\Order;
use App\Models\Player;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;

abstract class ApiController extends Controller
{
    /**
     * Notes   : 失败消息
     *
     * @Date   : 2022/12/23 21:48
     * @Author : <Jason.C>
     * @param  string  $message
     * @param  int  $code
     * @return JsonResponse
     */
    public function failed(
        string $message = '',
        int $code = 500
    ): JsonResponse {
        return $this->respond(
            code: $code,
            message: $message
        );
    }

    /**
     * Notes   : 结果返回
     *
     * @Date   : 2022/12/23 21:48
     * @Author : <Jason.C>
     * @param  mixed  $data
     * @param  int  $code
     * @param  string  $message
     * @param  array  $header
     * @return JsonResponse
     */
    protected function respond(
        mixed $data = '',
        int $code = 200,
        string $message = '',
        array $header = []
    ): JsonResponse {
        $response = [
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ];

        config('app.debug') && $response['time'] = number_format((microtime(true) - LARAVEL_START) * 1000, 2);

        return Response::json($response, 200, $header);
    }

    /**
     * Notes   : 验证码校验
     *
     * @Date   : 2024/7/30 上午10:21
     * @Author : <Jason.C>
     * @throws Exception
     */
    protected function verifyCaptcha(): void
    {
        $request = request();

        if ($request->player->season->config['show_captcha']) {
            $validator = Validator::make($request->all(), [
                'captcha_key' => 'required|bail|string',
                'captcha_val' => 'required|bail|captcha_api:'.request('captcha_key'),
            ], [
                'captcha_key.required' => '验证码必须',
                'captcha_key.string' => '验证码必须',
                'captcha_val.required' => '验证码必须',
                'captcha_val.captcha_api' => '验证码不正确',
            ]);

            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
        }
    }

    protected function orderResponse(Order $order, Player $player): JsonResponse
    {
        $params = [
            'app_key' => $player->application->app_key,
            'no' => $order->no,
        ];
        ksort($params);
        $params = array_merge($params, [
            'app_secret' => $player->application->app_secret,
        ]);
        $signature = strtoupper(md5(http_build_query($params)));

        return $this->success([
            'no' => $order->no,
            'url' => route('payments.wechat', [
                'no' => $order->no,
                'app_key' => $player->application->app_key,
                'signature' => $signature,
            ]),
        ]);
    }

    /**
     * Notes   : 成功数据
     *
     * @Date   : 2022/12/23 21:48
     * @Author : <Jason.C>
     * @param  mixed  $data
     * @return JsonResponse
     */
    public function success(mixed $data = ''): JsonResponse
    {
        return $this->respond(
            data: $data,
            message: 'SUCCESS'
        );
    }
}
